package com.shanwen.select.utils;

import com.shanwen.select.entity.Category;
import com.shanwen.select.entity.Menu;

import java.util.ArrayList;
import java.util.List;

/**
 * 把类别list转成树
 */
public class TreeUtils {

    //把一个List转成树
    public static List<Menu> bulidTree(List<Menu> list) {
        List<Menu> tree = new ArrayList<>();
        for (Menu node : list) {
            if (node.getParent() == 0) {
                tree.add(findChild(node, list));
            }
        }
        return tree;
    }

    static Menu findChild(Menu node, List<Menu> list) {
        for (Menu n : list) {
            if (n.getParent() == node.getId()) {
                if (node.getChildren() == null) {
                    node.setChildren(new ArrayList<Menu>());
                }
                node.getChildren().add(findChild(n, list));
            }
        }
        return node;
    }


}
